package com.bolt.common.utils;

import com.bolt.convention.data.DefaultPageInfo;
import com.bolt.convention.data.PageInfo;
import org.springframework.data.domain.Pageable;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2020/9/17.
 */
public class PageUtil {

    /**
     * List 分页
     */
    public static <T>List<T> toPage(int page, int size , List<T> list) {
        int fromIndex = page * size;
        int toIndex = page * size + size;
        if(fromIndex > list.size()){
            return new ArrayList();
        } else if(toIndex >= list.size()) {
            return list.subList(fromIndex,list.size());
        } else {
            return list.subList(fromIndex,toIndex);
        }
    }

    public static  <T>PageInfo< T> toPage(Pageable pageable, List<T> list){
       List<T> pageData = toPage(pageable.getPageNumber(),pageable.getPageSize(),list);
       return new DefaultPageInfo(pageable.getPageNumber() + 1, pageable.getPageSize(), pageData,list.size());
    }


}

 